<?php

namespace app\erp\controller;

use app\erp\model\Staffdb;
use app\erp\model\Departmentdb;

class Supplier extends Base
{
    protected $cols = array(
        array(
            'field' => 'member_id',
            'width' => 40,
            'title' => 'ID',
            'type' => 'checkbox',
            'fixed' => 'left'
        ),
        array(
            'field' => 'member_id',
            'width' => 60,
            'title' => 'id'
        ),
        array(
            'field' => 'supplier_name',
            'width' => 260,
            'title' => '供应商',
            'sort' => 'true'
        ),
        array(
            'field' => 'supplier_type',
            'width' => 70,
            'title' => '类型'
        ),
        array(
            'field' => 'username',
            'width' => 90,
            'title' => '登录名',
            'sort' => 'true'
        ),
        array(
            'field' => 'realname',
            'width' => 90,
            'title' => '联系人',
            'sort' => 'true'
        ),
        array(
            'field' => 'phone',
            'width' => 160,
            'title' => '联系电话',
            'sort' => 'true'
        ),
        /*array(
            'field' => 'be_staff_name',
            'width' => 160,
            'title' => '所属管理员'
        ),*/
        array(
            'field' => 'supplier_rate',
            'width' => 90,
            'title' => '折扣率%'
        ),
        array(
            'field' => 'contract_state',
            'width' => 100,
            'title' => '合同状态',
            'templet' => '#contract_stateTpl'
        ),
        array(
            'field' => 'hezuozhuangtai',
            'width' => 90,
            'title' => '合作状态',
            'templet' => '#hezuozhuangtaiTpl'
        ),
        array(
            'field' => 'wxgroup_name',
            'width' => 160,
            'title' => '微信群名'
        ),
        array(
            'field' => 'trade_no',
            'width' => 120,
            'title' => '税号'
        ),
        array(
            'field' => 'daibiao',
            'width' => 90,
            'title' => '签约代表'
        ),
        array(
            'field' => 'dianhua',
            'width' => 120,
            'title' => '电话号码'
        ),
        array(
            'field' => 'yinhang',
            'width' => 120,
            'title' => '开户银行'
        ),
        array(
            'field' => 'zhanghao',
            'width' => 160,
            'title' => '银行账号'
        ),
        array(
            'field' => 'danweidizhi',
            'width' => 200,
            'title' => '单位地址'
        ),
        array(
            'field' => 'yewuyuan',
            'width' => 90,
            'title' => '业务员'
        ),
        array(
            'field' => 'neiqin',
            'width' => 90,
            'title' => '内勤'
        ),
        array(
            'field' => 'bumenlaiyuan',
            'width' => 90,
            'title' => '部门来源'
        ),
       
        array(
            'field' => 'last_login_time',
            'width' => 120,
            'title' => '上次登录时间'
        ),array(
            'field' => 'last_ip',
            'width' => 120,
            'title' => '上次登录IP'
        ),
        array(
            'field' => 'create_time',
            'width' => 120,
            'title' => '录入时间',
            'sort' => 'true'
        ),
        
        array(
            'fixed' => 'right',
            'width' => 120,
            'toolbar' => '#supplier_tool'
        )
    );
    public $post;

    public function _initialize()
    {
        parent::_initialize();
        $request = request();
        $param = $request->param();
        $this->post = $param;
        if (isset ($this->post ['staff_id'])) {
            $this->post ['member_id'] = $this->post ['staff_id'];
            unset ($this->post ['staff_id']);
        }
        if (isset ($this->post ['_o'])) {
            $this->post ['_o'] = str_replace('_text', '', $this->post ['_o']);
        }
    }

    public function index()
    {
        $this->assign('cols', json_encode($this->cols));
        return $this->fetch();
    }

    public function ajaxList()
    {
        $staffdb = new Staffdb ();
        $this->post ['is_staff'] = 'yes';
        $staffdb->param = $this->post;
        $staffdb->param ['is_supplier'] = 1;
        $staffdb->param['site_id'] = 117;
        $return = $staffdb->ajaxSelect();
        $this->layuiReturn($return ['data'], $return ['count']);
    }

    public function ajaxMember()
    {
        $staffdb = new Staffdb ();
        $staffdb->param = $this->post;
        $return = $staffdb->ajaxSelect();
        $this->layuiReturn($return ['data'], $return ['count']);
    }

    public function tplForm()
    {
        $member_id = '';
        $data = array();
        $staffdb = new Staffdb ();
        if (isset ($this->post ['member_id']) && !empty ($this->post ['member_id'])) {
            // 获取数据
            $staffdb->param = $this->post;
            $data = $staffdb->ajaxFind();
            unset ($data ['password']);
            $member_id = $this->post ['member_id'];
        }
        $this->assign('data', json_encode($data));
        $this->assign('staff_id', $member_id);
        $this->assign('be_staff_id',$data['be_staff_id']??[]);

        // 获取员工
        $get_staff = $staffdb
            ->useGlobalScope(false)
            ->where('site_id',117)
            ->whereNotNull('department_id')
            ->where('is_supplier', 0)
            ->whereNull('delete_time')
            ->field('member_id,username')
            ->select();
        $this->assign('get_staff', $get_staff);
        return $this->fetch();
    }

    /**
     *
     * @todo 保存数据
     */
    public function saveData()
    {
        $staffdb = new Staffdb ();
        $staffdb->param = $this->post;
        $staffdb->param ['is_manage'] = 2;
        if(!isset($this->post['be_staff_id'])){
            //$staffdb->param ['be_staff_id'] = [];
            $this->layuiReturn('请勾选所属管理员', 0, '请勾选所属管理员', -1);
        }
        $return = $staffdb->ajaxSave();
        if ($return != 'success') {
            $this->layuiReturn($return, 0, $return, -1);
        } else {
            $this->layuiReturn('保存成功');
        }
    }

    /**
     *
     * @todo 获取单条数据
     */
    public function ajaxOneData()
    {
        $staffdb = new Staffdb ();
        $staffdb->param = $this->post;
        $data = $staffdb->ajaxFind();
        $this->layuiReturn($data, 1);
    }

    /**
     *
     * @todo 软删除
     */
    public function ajaxDelete()
    {
        $staff = new Staffdb ();
        $staff->param = $this->post;
        $staff->ajaxDelete();
        $this->layuiReturn('删除成功', 0);
    }

    public function ajaxRestore()
    {
        $staff = new Staffdb ();
        $staff->param = $this->post;
        $staff->ajaxRestore();
        $this->layuiReturn('数据恢复成功', 0);
    }

    // 回收站
    public function recycle()
    {
        array_push($this->cols, array(
            'field' => 'delete_time',
            'width' => 120,
            'title' => '删除时间'
        ), array(
            'fixed' => 'right',
            'width' => 80,
            'toolbar' => '#recycle_staff_tool'
        ));
        $this->assign('cols', json_encode($this->cols));
        return $this->fetch();
    }

    public function getCityDepartment()
    {
        $departmentdb = new Departmentdb ();

        $param = array(
            'site_id' => $this->post ['site_id'],
            '_o' => 'department_id',
            '_b' => 'desc',
            'page' => 1,
            'limit' => 30
        );
        $departmentdb->param = $param;
        $data = $departmentdb->ajaxSelect();
        $this->layuiReturn($data ['data'], $data ['count']);
    }
}